
package backupUtility;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.swing.JOptionPane;
/**
 * This class contains the logic behind the BackupUtilityUI.java
 * it is responsible for how the backup files are created, stored
 * and locked into a folder. This class implements the ZipBackup.java
 * which in turn, creates an archive file for the backup folder
 * @author Changcoco Kevin Paolo
 * @author Mamis Dominique Bernard
 * @author Palasi Jandy
 * @author Rafael Ian Paolo 
 */

public class BackupLogic {
/**
* Creates a new directory
* Copies the file to the directory
* Includes the File name and the password
* Includes file Zip
* Compresses file after files was relocated to directory
* Asks whether to add in existing file or create new directory
* Gets the files from the array FileGetter
*
* @throws IOException			Throws exception when file already exist
* @param ArrayList<File>		Array of files
* @param String folder			Directory folder
* @param String password		Input Password
*/
    public BackupLogic(ArrayList<File> selected, String folder, String password) throws IOException{
    String fName = "C:/BACKUP/"+folder+"/";
    File directory = new File(fName);
    if (!directory.exists()) {
           JOptionPane.showMessageDialog(null,"Creating directory: " +directory.getName());
           boolean result = directory.mkdirs();  
            if(result) {    
              JOptionPane.showMessageDialog(null,"Backup created");  
            }
           ZipBackup zip = new ZipBackup(selected,fName, fName+""+folder+".zip", password);
    }else{
        int reply = JOptionPane.showConfirmDialog(null,"Do you want to add?", "Folder " + directory.getName() + " already exist!",   JOptionPane.YES_NO_OPTION);
            if (reply == JOptionPane.YES_OPTION){
                 JOptionPane.showMessageDialog(null,"Adding: " + directory.getName());
                 boolean result = directory.mkdirs();  
                 if(result) {    
                    JOptionPane.showMessageDialog(null,"Backup successful!");  
                 }
                 ZipBackup zip = new ZipBackup(selected,fName, fName+""+folder+".zip", password);
            }else if(reply == JOptionPane.NO_OPTION){
                
            }
    }
    
    }
/**
* Copies the files using Java 7 files
* Includes the file source and file destination
* Copy files from file source and to file destination
*
* @throws IOExeption			throws exception when file already exist
* @param File source			Where to get the files
* @param File dest				Where to copy/save the files
*/
    private void copyFileUsingJava7Files(File source, File dest) throws IOException {
    Files.copy(source.toPath(), dest.toPath());
    }
    
}
